package com.itheima.util;

import java.util.HashMap;
import java.util.Map;

public class VerificationCodeCache {
    private static final Map<String, Long> emailSentTime = new HashMap<>();
    private static final Map<String, String> emailVerificationCodes = new HashMap<>();

    // 验证码防刷校验
    public static boolean canSendVerificationCode(String email) {
        long currentTime = System.currentTimeMillis();
        if (emailSentTime.containsKey(email)) {
            long lastSentTime = emailSentTime.get(email);
            // 设置间隔时间为60秒，即每个邮箱每分钟只能请求一次验证码
            if (currentTime - lastSentTime < 60000) {
                return false; // 还未到下次发送时间
            }
        }
        return true;
    }

    // 保存验证码到缓存
    public static void saveVerificationCode(String email, String code) {
        emailSentTime.put(email, System.currentTimeMillis());
        emailVerificationCodes.put(email, code);
    }

    // 获取验证码
    public static String getVerificationCode(String email) {
        return emailVerificationCodes.get(email);
    }
}
